/**
 * Service
 */
var $ = require('jquery');
var myService = angular.module("myApp.service",[]);
myService.service('expenseService', ["$http", function ($http) {
	var baseUrl = "/expense";
	this.queryExpenseInfo = function(custId){
		return $http.post(baseUrl + "/queryExpenseInfo/1");
	}
	
}]).service('performanceService', ["$http", function ($http) {
	var baseUrl = "/performance";
	this.queryMyProject = function(custId){
		var param = {};
		if(custId != null && custId != ''){
			param = {custId : custId};
		}
		return $http({
			method : 'POST',
			url : baseUrl + "/queryMyProject",
			data : $.param(param), // pass in data as strings
			headers : {
				'Content-Type' : 'application/x-www-form-urlencoded'
			}
		})
	}
	this.queryMyReward = function(custId){
		return $http.post(baseUrl + "/queryMyReward");
	}
	
}]).service('pointService', ["$http", function ($http) {
	var baseUrl = "/point";
	this.queryAccountInfo = function(){
		return $http.post(baseUrl + "/queryAccountInfo");
	}
	
}]).service('recruitmentService', ["$http", function ($http) {
	var baseUrl = "/recruitment";
	this.queryRecommendInfo = function(custId){
		var param = {};
		if(custId != null && custId != ''){
			param = {custId : custId};
		}
		return $http({
			method : 'POST',
			url : baseUrl + "/queryRecommendInfo",
			data : $.param(param), // pass in data as strings
			headers : {
				'Content-Type' : 'application/x-www-form-urlencoded'
			}
		})
	}
	this.recommendCount = function(custId){
		return $http.post(baseUrl + "/recommendCount");
	}

    this.queryCoolRecommend = function(){
        return $http.post(baseUrl + "/coolRecommend");
    }
	
}]).service('checkAuthService', ["$http", function ($http) {
	this.checkAuthCode = function(code){
		return $http.post("/checkOpenId/" + code);
	}
	
}]).factory('LoadingDataProject', ["$http", function($http) {
	var LoadingData = function(url, param) {
	    this.items = [];
	    this.busy = false;
	    this.page = 1;
	    this.hasMoreData = true;
	    this.hasData = true;
	    this.url = url;
	    this.length = 10;
	    this.param = param;
	};
	LoadingData.prototype.nextPage = function() {
	    if (this.busy) return;
	    this.busy = true;
	    if(this.hasMoreData){
			$http({
				method : 'POST',
				url : this.url+this.page,
				data : $.param(this.param), // pass in data as strings
				headers : {
				'Content-Type' : 'application/x-www-form-urlencoded'
				}
			}).success(function(result) {
					var items = result.data.data;
					var innerLength = 0;
					for (var i = 0; i < items.length; i++) {
						this.items.push(items[i]);
					}
					for (var i = 0; i < items.length; i++) {
						innerLength += items[i].data.length;
					}
					if(innerLength < this.length){
						this.hasMoreData = false;
					}
					if(items.length == 0){
						this.hasData = false;
					}
					this.busy = false;
					this.page += 1;
			    }.bind(this));
	    }else{
	    	this.busy = false;
	    }
	};
	return LoadingData;
}]).factory('LoadingData', ["$http", function($http) {
	var LoadingData = function(url, param) {
	    this.items = [];
	    this.busy = false;
	    this.page = 1;
	    this.hasMoreData = true;
	    this.hasData = true;
	    this.url = url;
	    this.length = 10;
	    this.param = param;
	};
	LoadingData.prototype.nextPage = function() {
	    if (this.busy) return;
	    this.busy = true;
	    if(this.hasMoreData){
			$http({
				method : 'POST',
				url : this.url+this.page,
				data : $.param(this.param), // pass in data as strings
				headers : {
				'Content-Type' : 'application/x-www-form-urlencoded'
				}
			}).success(function(result) {
					var items = result.data.data;
					for (var i = 0; i < items.length; i++) {
						this.items.push(items[i]);
					}
					if(items.length < this.length){
						this.hasMoreData = false;
					}
					if(items.length == 0){
						this.hasData = false;
					}
					this.busy = false;
					this.page += 1;
			    }.bind(this));
	    }else{
	    	this.busy = false;
	    }
	};
	return LoadingData;
}]);